package com.unitech.drinks.controller;

import com.unitech.drinks.exception.ErrorResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;

/**
 * http异常的全局处理
 */
@Slf4j
@RestControllerAdvice
public class HttpExceptionHandler {

    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseStatus(code = HttpStatus.BAD_REQUEST)
    public ErrorResponse handleError(HttpMessageNotReadableException e) {
        log.error("HTTP请求body缺失", e);
        return ErrorResponse
                .builder()
                .code(HttpServletResponse.SC_BAD_REQUEST)
                .message("HTTP请求body缺失")
                .build();
    }

    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseStatus(code = HttpStatus.METHOD_NOT_ALLOWED)
    public ErrorResponse handleError(HttpRequestMethodNotSupportedException e) {
        log.error("HTTP请求方式不支持", e);
        return ErrorResponse
                .builder()
                .code(HttpServletResponse.SC_METHOD_NOT_ALLOWED)
                .message("HTTP请求方式不支持")
                .build();
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(code = HttpStatus.NOT_FOUND)
    public ErrorResponse handleError(NoHandlerFoundException e) {
        log.error("HTTP请求不存在", e);
        return ErrorResponse
                .builder()
                .code(HttpServletResponse.SC_NOT_FOUND)
                .message("HTTP请求不存在")
                .build();
    }

    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    @ResponseStatus(code = HttpStatus.UNSUPPORTED_MEDIA_TYPE)
    public ErrorResponse handleError(HttpMediaTypeNotSupportedException e) {
        log.error("HTTP请求类型不匹配", e);
        return ErrorResponse
                .builder()
                .code(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE)
                .message("HTTP请求类型不匹配")
                .build();
    }

    @ExceptionHandler(BindException.class)
    @ResponseStatus(code = HttpStatus.BAD_REQUEST)
    public ErrorResponse handleError(BindException e) {
        log.error("HTTP请求参数绑定异常", e);
        FieldError error = e.getFieldError();
        String message = String.format("%s:%s", error.getField(), error.getDefaultMessage());
        return ErrorResponse
                .builder()
                .code(HttpServletResponse.SC_BAD_REQUEST)
                .message(message)
                .build();
    }

    @ExceptionHandler(SQLException.class)
    @ResponseStatus(code = HttpStatus.BAD_REQUEST)
    public ErrorResponse handleError(SQLException e) {
        log.error("数据库操作异常", e);
        return ErrorResponse
                .builder()
                .code(HttpServletResponse.SC_BAD_REQUEST)
                .message("操作失败")
                .build();
    }


}
